package models

import (
	"github.com/beego/beego/v2/adapter/orm"
)

type Services struct {
	ServiceID         int     `orm:"column(service_ID)"`
	ServiceName       string  `orm:"column(service_name)"`
	ServiceStatus     int     `orm:"column(service_status)"`
	ServiceScore      int     `orm:"column(service_score)"`
	ServiceAssessment string  `orm:"column(service_assessment)"`
	StartTime         string  `orm:"column(start_time)"`
	EndTime           string  `orm:"column(end_time)"`
	BreakTime         string  `orm:"column(break_time)"`
	ServiceBudget     float32 `orm:"column(service_budget)"`
	CustomerID        int     `orm:"column(customer_ID)"`
}

func init() {
	orm.RegisterModel(new(Services))
}

type ServiceSubmitModel struct {
	ServiceName   string  `json:"serviceName" orm:"column(service_name)"`
	ServiceBudget float32 `json:"serviceBudget" orm:"column(service_budget)"`
	CustomerID    int     `json:"customerID" orm:"column(customer_ID)"`
}

func AddService(submit ServiceSubmitModel) error {
	o := orm.NewOrm()
	err := o.Begin()
	_, err = o.Raw("INSERT INTO services (service_name, service_budget, customer_ID, start_time) VALUES (?, ?, ?, NOW())", submit.ServiceName, submit.ServiceBudget, submit.CustomerID).Exec()
	_, err = o.Raw("update customers set customer_status = 1 ,service_times = service_times + 1 where customer_ID = ?", submit.CustomerID).Exec()
	if err != nil {
		err = o.Rollback()
		return err
	} else {
		err = o.Commit()
		return nil
	}
}

type ServiceVoModel struct {
	ServiceID     int     `json:"serviceID" orm:"column(service_ID)"`
	ServiceName   string  `json:"serviceName" orm:"column(service_name)"`
	StartTime     string  `json:"startTime" orm:"column(start_time)"`
	ServiceStatus int     `json:"serviceStatus" orm:"column(service_status)"`
	ServiceBudget float32 `json:"serviceBudget" orm:"column(service_budget)"`
}

// GetServiceListInformationByCustomerID 查询客户下服务信息列表
func GetServiceListInformationByCustomerID(customerID int) ([]ServiceVoModel, int64) {
	o := orm.NewOrm()
	var services []ServiceVoModel
	num, err := o.Raw("select ser.service_ID,ser.service_name,ser.start_time,ser.service_status,ser.service_budget from services ser where ser.customer_ID = ?", customerID).QueryRows(&services)
	if err == nil {
		return services, num
	} else {
		return []ServiceVoModel{}, num
	}
}

// ServiceViewModel 服务详细信息模板
type ServiceViewModel struct {
	ServiceID         int     `json:"serviceID" orm:"column(service_ID)"`
	ServiceName       string  `json:"serviceName" orm:"column(service_name)"`
	ServiceStatus     int     `json:"serviceStatus" orm:"column(service_status)"`
	ServiceScore      int     `json:"serviceScore" orm:"column(service_score)"`
	ServiceAssessment string  `json:"serviceAssessment" orm:"column(service_assessment)"`
	ServiceBudget     float32 `json:"serviceBudget" orm:"column(service_budget)"`
}

func GetServiceViewByServiceID(serviceID int) (ServiceViewModel, error) {
	o := orm.NewOrm()
	var serviceView ServiceViewModel
	err := o.Raw("select service_ID,service_name,service_status,service_score,service_assessment,service_budget from services where service_ID = ?", serviceID).QueryRow(&serviceView)
	if err == nil {
		return serviceView, nil
	} else {
		return ServiceViewModel{}, err
	}

}
